package com.kuanter.kuanterauto.chexian;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.MainActivity;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.MapResult;
import com.kuanter.kuanterauto.entity.VehicleInfo;
import com.kuanter.kuanterauto.entity.VehicleInfoList;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.umeng.analytics.MobclickAgent;

/**
 * 车险界面
 * 
 * @author soup
 * 
 */
public class ChexianActivity extends BaseActivity_NoBar {

	private ListView lv_chexian = null;
	private List<VehicleInfo> vehicleInfos = null;
	private Adapter adapter = null;
	DataLoader dataLoader = null;
	public static final int XGCL = 1;
	MapResult mapR = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.chexian);
		mapR = MapResult.getMapResults();
		if (CheckNetworkState()) {
			showProgress("数据加载中...");

			initView();
			dataLoader = new DataLoader(this);
			List<NameValuePair> params3 = new ArrayList<NameValuePair>();
			params3.add(new BasicNameValuePair("vehicleInfo.userId",
					MainActivity.ID + ""));
			dataLoader.DisplayData(MainConstants.HOST
					+ "vehicleInfo/query_vehicle", params3, handler,
					"vehicleInfo/query_vehicle", false);
			// }
		}

	}

	private void initView() {
		lv_chexian = (ListView) findViewById(R.id.lv_chexian);
		lv_chexian.setOnItemClickListener(null);

		// if(vehicleInfos!=null&&vehicleInfos.size()>0){
		// System.out.println("lv");
		// adapter=new Adapter(this, vehicleInfos);
		// lv_chexian.setAdapter(adapter);
		// }

		// vehicleInfos.
	}

	@Override
	public void onClick(View v) {

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		try {
			switch (resultCode) {
			case XGCL:
				if (data.getBooleanExtra("update", false)) {
					// vehicleInfos.remove(data.getIntExtra("index", 0));
					// vehicleInfos.add(data.getIntExtra("index", 0),
					// (VehicleInfo)
					// data.getSerializableExtra(VehicleInfoList.TAG));
					// if(adapter!=null){
					// adapter = new Adapter(ChexianActivity.this,
					// vehicleInfos);
					// lv_chexian.setAdapter(adapter);
					// }
					mapR.removeValueForKey("vehicleInfo/query_vehicle");
				}
				// adapter.notifyDataSetChanged();
				break;
			}
		} catch (Exception e) {
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		super.onRestart();

	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				VehicleInfoList vehicleInfoList = new VehicleInfoList(msg
						.getData().getString("result"));
				vehicleInfos = vehicleInfoList.getVehicleInfoList();
				if (vehicleInfos != null && vehicleInfos.size() > 0) {
					adapter = new Adapter(ChexianActivity.this, vehicleInfos);
					lv_chexian.setAdapter(adapter);
					lv_chexian.setVisibility(View.VISIBLE);
				}
				finishProgress();
				break;
			case HttpTask.RESULT_ERROR:
				showToast("服务器连接失败");
				finishProgress();
				break;
			}
		}
	};

	class Adapter extends BaseAdapter {

		private List<VehicleInfo> vehicleInfos = null;
		private Context context = null;

		public Adapter(Context context, List<VehicleInfo> vehicleInfos) {
			this.context = context;
			this.vehicleInfos = vehicleInfos;
		}

		@Override
		public int getCount() {
			return vehicleInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return vehicleInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		// 投保热线
		public String callTB(String company) {
			if (company.equals("华泰保险")) {
				return "4006012345";
			} else if (company.equals("天安保险")) {
				return "95505";
			} else if (company.equals("中国平安")) {
				return "4008000000";
			} else if (company.equals("中国人保")) {
				return "4001234567";
			} else if (company.equals("大地保险")) {
				return "95590";
			} else if (company.equals("太平洋保险")) {
				return "10108888";
			} else if (company.equals("阳光保险")) {
				return "4000000000";
			} else if(company.equals("太平财险")){
				return "4008688888";
			} else if(company.equals("紫金财险")){
				return "10108080";
			} else if(company.equals("天平汽车保险")){
				return "95550";
			} else if(company.equals("中华联合")){
				return "4001999999";
			} else if(company.equals("长安保险")){
				return "59595592";
			} else {
				return "";
			}
		}

		// 报险热线
		public String callBX(String company) {
			if (company.equals("华泰保险")) {
				return "4006095509";
			} else if (company.equals("天安保险")) {
				return "95505";
			} else if (company.equals("中国平安")) {
				return "95511";
			} else if (company.equals("中国人保")) {
				return "95518";
			} else if (company.equals("大地保险")) {
				return "95590";
			} else if (company.equals("太平洋保险")) {
				return "95500";
			} else if (company.equals("阳光保险")) {
				return "95510";
			} else if(company.equals("太平财险")){
				return "95529";
			} else if(company.equals("紫金财险")){
				return "4008280018";
			} else if(company.equals("天平汽车保险")){
				return "95550";
			} else if(company.equals("中华联合")){
				return "95585";
			} else if(company.equals("长安保险")){
				return "95592";
			} else {
				return "";
			}
		}

		// 设置电话分割线
		public String setNum(String num) {
			if (num.equals("4006012345")) {
				return "400-6012-345";
			} else if (num.equals("4006095509")) {
				return "400-6095-509";
			} else if (num.equals("4008000000")) {
				return "400-8000-000";
			} else if (num.equals("4001234567")) {
				return "400-1234-567";
			} else if (num.equals("4000000000")) {
				return "400-0000-000";
			} else if (num.equals("4008688888")){
				return "400-8688-888";
			} else if (num.equals("4008280018")){
				return "400-8280-018";
			} else if (num.equals("4001999999")){
				return "400-1999-999";
			} else {
				return num;
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.lv_chexian_item, null);
				holder.tv_clxx = (TextView) convertView
						.findViewById(R.id.tv_clxx);
				holder.tv_ppxx = (TextView) convertView
						.findViewById(R.id.tv_ppxx);
				holder.tv_bxxx = (TextView) convertView
						.findViewById(R.id.tv_bxxx);
				holder.tv_dqsj = (TextView) convertView
						.findViewById(R.id.tv_dqsj);

				holder.rl_xgcl = (RelativeLayout) convertView
						.findViewById(R.id.rl_xgcl);
				holder.rl_yjxb = (RelativeLayout) convertView
						.findViewById(R.id.rl_yjxb);
				holder.rl_yjba = (RelativeLayout) convertView
						.findViewById(R.id.rl_yjba);

				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			// {"status":true,
			// "vehicleInfoList":
			// [{"id":95559,"insuranceCity":"武汉","insuranceCompany":"大地保险",
			// "insuranceStartTime":"2013-02-26","licensePlateNumber":"渝A11555","vehicleBrand":"阿尔法·罗米欧","vehicleModels":"156"}]}
			// holder.tv_clxx.setText(vehicleInfos.get(position).getVehicleBrand()
			// + "——" + vehicleInfos.get(position).getVehicleModels());
			holder.tv_ppxx.setText(vehicleInfos.get(position).getVehicleBrand()
					.equals("") ? "暂无信息 — " : vehicleInfos.get(position)
					.getVehicleBrand() + " — ");
			holder.tv_ppxx.append(vehicleInfos.get(position).getVehicleModels()
					.equals("") ? "暂无信息" : vehicleInfos.get(position)
					.getVehicleModels());
			// tv_ppxx
			// holder.tv_ppxx.setText(vehicleInfos.get(position).getLicensePlateNumber().substring(0,
			// 2)+ "——"
			// +
			// vehicleInfos.get(position).getLicensePlateNumber().substring(2,vehicleInfos.get(position).getLicensePlateNumber().length()));
			String str = vehicleInfos.get(position).getLicensePlateNumber()
					.substring(0, 2).equals("") ? "暂无信息 — " : vehicleInfos
					.get(position).getLicensePlateNumber().substring(0, 2);
			String str1 = vehicleInfos
					.get(position)
					.getLicensePlateNumber()
					.substring(
							2,
							vehicleInfos.get(position).getLicensePlateNumber()
									.length()).equals("") ? "暂无信息"
					: vehicleInfos
							.get(position)
							.getLicensePlateNumber()
							.substring(
									2,
									vehicleInfos.get(position)
											.getLicensePlateNumber().length());
			if (!str.equals("暂无信息 — ")) {
				if (ChexianActivity.this.isHanZi(str.substring(0, 1))
						|| ChexianActivity.this.isE(str.substring(1, 1))) {
					holder.tv_clxx.setText(str + " — ");// tv_clxx
					holder.tv_clxx.append(str1);
				} else {
					holder.tv_clxx.setText("暂无信息 — ");
					if (!str1.equals("暂无信息")) {
						holder.tv_clxx.append(vehicleInfos.get(position)
								.getLicensePlateNumber());
					} else {
						holder.tv_clxx.append("暂无信息");
					}
				}
			} else {
				holder.tv_clxx.setText(str);
			}

			// holder.tv_ppxx.setText(vehicleInfos.get(position).getLicensePlateNumber().substring(0,1).equals("")?"暂无信息 — ":vehicleInfos.get(position).getLicensePlateNumber().substring(0,1)+
			// " — ");
			// holder.tv_ppxx.append(vehicleInfos.get(position).getLicensePlateNumber().substring(1,vehicleInfos.get(position).getLicensePlateNumber().length()).equals("")?"暂无信息":vehicleInfos.get(position).getLicensePlateNumber().substring(1,vehicleInfos.get(position).getLicensePlateNumber().length()));

			// if (!vehicleInfos.get(position).getInsuranceCity().equals("null")
			// || !vehicleInfos.get(position).getInsuranceCompany()
			// .equals("null")) {
			if (vehicleInfos.get(position).getInsuranceCity().equals("null")
					|| vehicleInfos.get(position).getInsuranceCity().equals("")) {
				holder.tv_bxxx.setText("城市 — ");
			} else {
				holder.tv_bxxx.setText(vehicleInfos.get(position)
						.getInsuranceCity() + " — ");
			}

			if (vehicleInfos.get(position).getInsuranceCompany().equals("null")
					|| vehicleInfos.get(position).getInsuranceCompany()
							.equals("")) {
				holder.tv_bxxx.append("保险");
			} else {
				holder.tv_bxxx.append(vehicleInfos.get(position)
						.getInsuranceCompany());
			}

			// if(vehicleInfos.get(position).getInsuranceCompany().equals("null")){
			// holder.tv_bxxx.append(vehicleInfos.get(position).getInsuranceCompany().equals("null")?"暂无信息":vehicleInfos.get(position).getInsuranceCompany());
			// }else{
			// holder.tv_bxxx.append(vehicleInfos.get(position).getInsuranceCompany().equals("null")?"暂无信息":vehicleInfos.get(position).getInsuranceCompany());
			// }
			//
			// if(vehicleInfos.get(position).getInsuranceCity().equals("")){
			// holder.tv_bxxx.setText(vehicleInfos.get(position).getInsuranceCity().equals("")?"暂无信息 — ":vehicleInfos.get(position).getInsuranceCity()+
			// " — ");
			// }else{
			// holder.tv_bxxx.setText(vehicleInfos.get(position).getInsuranceCity().equals("")?"暂无信息 — ":vehicleInfos.get(position).getInsuranceCity()+
			// " — ");
			// }
			//
			// if(vehicleInfos.get(position).getInsuranceCompany().equals("")){
			// holder.tv_bxxx.append(vehicleInfos.get(position).getInsuranceCompany().equals("")?"暂无信息":vehicleInfos.get(position).getInsuranceCompany());
			// }else{
			// holder.tv_bxxx.append(vehicleInfos.get(position).getInsuranceCompany().equals("")?"暂无信息":vehicleInfos.get(position).getInsuranceCompany());
			// }

			// } else {
			// holder.tv_bxxx.setText("——");
			// }
			String date[] = vehicleInfos.get(position).getInsuranceStartTime()
					.split("-");
			if (date.length == 3) {
				holder.tv_dqsj.setText(date[0] + "年" + date[1] + "月" + date[2]
						+ "日");
			} else {
				holder.tv_dqsj.setText("暂无信息");
			}

			final int p = position;

			final Holder h = holder;
			// 启动修改车辆
			holder.rl_xgcl.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ChexianActivity.this,
							XgclActivity.class);
					intent.putExtra(VehicleInfoList.TAG, vehicleInfos.get(p));
					intent.putExtra("index", p);
					ChexianActivity.this.startActivityForResult(intent, XGCL);
				}
			});
			// 启动一键续保
			holder.rl_yjxb.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (!vehicleInfos.get(p).getInsuranceCompany().equals("")
							&& !vehicleInfos.get(p).getInsuranceCompany()
									.equals("null")) {
						AlertDialog.Builder builder = new AlertDialog.Builder(
								ChexianActivity.this);
						builder.setTitle(setNum(callTB(h.tv_bxxx.getText()
								.toString().split(" — ")[1])));
						builder.setPositiveButton("呼叫",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										ChexianActivity.this
												.call(callTB(h.tv_bxxx
														.getText().toString()
														.split(" — ")[1]));
									}
								});

						builder.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
									}
								});

						builder.create().show();
					} else {
						ChexianActivity.this.showToast("暂无信息,无法拨打电话");
					}
				}
			});
			// 启动一键报案
			holder.rl_yjba.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (!vehicleInfos.get(p).getInsuranceCompany().equals("")
							&& !vehicleInfos.get(p).getInsuranceCompany()
									.equals("null")) {
						AlertDialog.Builder builder = new AlertDialog.Builder(
								ChexianActivity.this);
						builder.setTitle(setNum(callBX(h.tv_bxxx.getText()
								.toString().split(" — ")[1])));
						builder.setPositiveButton("呼叫",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
										ChexianActivity.this
												.call(callBX(h.tv_bxxx
														.getText().toString()
														.split(" — ")[1]));
									}
								});

						builder.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface arg0,
											int arg1) {
									}
								});

						builder.create().show();
					} else {
						ChexianActivity.this.showToast("暂无信息,无法拨打电话");
					}
				}
			});

			return convertView;
		}

		class Holder {
			TextView tv_clxx;
			TextView tv_ppxx;
			TextView tv_bxxx;
			TextView tv_dqsj;
			RelativeLayout rl_xgcl;
			RelativeLayout rl_yjxb;
			RelativeLayout rl_yjba;

		}

	}

}
